package http

import (
	"net/http"
	"send-id-num/pkg/logger"
	"sync"
	"time"
)

func NewHttpServer(wg *sync.WaitGroup, port string, router http.Handler) *http.Server {
	srv := &http.Server{
		Addr:         port,
		Handler:      router,
		ReadTimeout:  20 * time.Second,
		WriteTimeout: 20 * time.Second,
		IdleTimeout:  20 * time.Second,
	}
	go func() {
		defer wg.Done()
		if err := srv.ListenAndServe(); err != http.ErrServerClosed {
			logger.Log.Error("http.server "+port[1:]+" start failed ,", err)
		} else {
			logger.Log.Info("http.server " + port[1:] + " exit complete...")
		}
	}()
	return srv
}
